﻿using BCCommon;
using BCCommon.TMS.Driver;
using BCDto.Common.ErrorMessage;
using BCDto.TMS.Driver.DriverLearnDatum;
using BCService.TMS.Driver.DriverLearnDatum;
using BCTMSRESTful.Configs;
using BCTMSRESTful.Controllers.Management;
using Microsoft.AspNetCore.Mvc;

namespace BCTMSRESTful.Controllers.Management.Driver
{
    /// <summary>
    /// 司机学习资料接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class DriverLearnDatumsController : ManagementBaseController
    {
        private readonly IDriverLearnDatumService driverLearnDatumService;

        public DriverLearnDatumsController(IDriverLearnDatumService driverLearnDatumService)
        {
            this.driverLearnDatumService = driverLearnDatumService;
        }

        /// <summary>
        /// 带有分页信息的查询学习资料
        /// </summary>
        /// <param name="answerContent">答案内容</param>
        /// <param name="appliedTo">适用业务类型(司机、骑手)</param>
        /// <param name="datumTopic">学习资料题目</param>
        /// <param name="index">页码</param>
        /// <param name="size">页数</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<DriverLearnDatumDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult GetDriverLearnDatums(
            [FromQuery(Name = "datum_topic")] string datumTopic,
            [FromQuery(Name = "answer_content")] string answerContent,
            [FromQuery(Name = "applied_to")] DriverRoleType? appliedTo,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            return Ok(driverLearnDatumService.GetDriverLearnDatums(datumTopic, answerContent, appliedTo, index, size));
        }

        /// <summary>
        /// 新增一个学习资料
        /// </summary>
        /// <param name="dto">接收学习资料信息</param>
        /// <returns></returns>
        [HttpPost]
        [Log("新增司机学习资料")]
        [ProducesResponseType(typeof(DriverLearnDatumAnswerViewDto), 201)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Post([FromBody] DriverLearnDatumRequestDto dto)
        {
            var result = driverLearnDatumService.Add(this.Admin, dto);
            return Created(string.Format("/api/v1/sim/DriverLearnDatums/{0}", result.LearnDatum.DatumId), result);
        }

        /// <summary>
        /// 查询指定的学习资料
        /// </summary>
        /// <param name="datumId">学习资料编号</param>
        /// <response code="200">返回学习资料信息及答案。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器处理请求时发生错误。</response>
        /// <returns></returns>
        [HttpGet("{datum_id}")]
        [ProducesResponseType(typeof(DriverLearnDatumAnswerViewDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Detail([FromRoute(Name = "datum_id")] int datumId)
        {
            return Ok(driverLearnDatumService.DriverLearnDatumByDatumIdWithAnswer(datumId));
        }

        /// <summary>
        /// 删除指定的学习资料
        /// </summary>
        /// <param name="datumId">学习资料编号</param>
        /// <response code="200">删除成功。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器处理请求时发生错误。</response>
        /// <returns></returns>
        [HttpDelete("{datum_id}")]
        [Log("删除司机学习资料")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Delete([FromRoute(Name = "datum_id")] int datumId)
        {
            driverLearnDatumService.Delete(datumId);
            return NoContent();
        }
    }
}
